package pl.kosmowska.ewelina.server.locators;

import javax.jdo.PersistenceManager;

import pl.kosmowska.ewelina.server.PMF;
import pl.kosmowska.ewelina.server.dao.UserDataDAO;
import pl.kosmowska.ewelina.server.jdo.DatabaseEntity;
import pl.kosmowska.ewelina.server.jdo.UserData;

import com.allen_sauer.gwt.log.client.Log;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class EntityStringLocator extends EntityLocator<String> {

  @Override
  public Class<String> getIdType() {
    return String.class;
  }
  
	public PersistenceManager getPersistenceManager() {
		return PMF.get().getPersistenceManager();
	}

	@Override
	public DatabaseEntity<String> find(
			Class<? extends DatabaseEntity<String>> clazz, String id) {
	    Long uid = UserDataDAO.getLoggedInUserId();
//	    if (id == null) {
//	      return null;
//	    }
	    Key key = KeyFactory.stringToKey((java.lang.String) id);
	    PersistenceManager pm = getPersistenceManager();
	    try {
	    	DatabaseEntity<String> e = pm.getObjectById(clazz, key);
//	      if (e.getUserId() != uid) { 
//	        e = null;
//	      }
	      return e;
	    } catch (Exception e) {
	     // e.printStackTrace();
	    } finally {
	      pm.close();
	    }
	    return null;
	}
  
}